package com.leoblog.action.base;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;

import com.leoblog.dao.base.BaseDao;
import com.leoblog.domain.base.BaseDOM;
import com.leoblog.service.base.BaseService;
import com.leoblog.util.GenericUtil;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("unchecked")
public abstract class BaseAction <T extends BaseDOM, M extends BaseDao<T>, N extends BaseService<T, M >> extends ActionSupport implements InitializingBean {
	/**
	 * Get log object.
	 */
	private static final Log log = LogFactory.getLog(BaseAction.class);
	/**
	 * Back url key.
	 */
	public static final String BACKURL = "backURL";
	/**
	 * Back url.
	 */
	private String backURL;
	/**
	 * Service.
	 */
	protected N service;
	/**
	 * Domain.
	 */
	protected T domain;
	/**
	 * Domain object's class.
	 */
	protected Class<T> domainClass; 

	public String getBackURL() {
		return backURL;
	}

	public void setBackURL(String backURL) {
		this.backURL = backURL;
	}
	
	/**
	 * Back to the back url.
	 * @return back result.
	 */
	public String backURL()
	{
		log.info("Forward to url: " + backURL);
		return BACKURL;
	}

	protected Class<T> getDomainClass() {
		return domainClass;
	}
	
	public N getService() {
		return service;
	}
	
	public void setService(N service) {
		this.service = service;
	}
	
	public T getDomain() {
		return domain;
	}
	
	public void setDomain(T domain) {
		this.domain = domain;
	}
	
	public void afterPropertiesSet() {
		// Set domainClass field.
		domainClass = GenericUtil.getSuperClassGenricType(getClass(), 0);
		// Set domain into service.
		service.setDomain(domain);
		log.info("BaseAction domainClass" + domainClass);
	}
}
